package com.zyf.pattern.proxy.dbroute;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * create by yifeng
 */
public class OrderServiceDynamicProxy implements InvocationHandler {

    Object proxyObj;

    public Object getInstance(Object proxyObj) {
        this.proxyObj = proxyObj;
        Class<?> clazz = proxyObj.getClass();
        
        return Proxy.newProxyInstance(clazz.getClassLoader(),  clazz.getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before(args[0]);
        Object object = method.invoke(proxyObj, args);
        after();
        return null;
    }

    private void after() {
        // 还原默认的数据源
    }

    // target 应该是订单对象order
    private void before(Object target) {
        // 进行数据源的切换
    }
}
